package question;

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }



    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode cur1 = l1, cur2 = l2;
            ListNode newHead = new ListNode(0); // 创建⼀个虚拟头结点，⽅便记录结果
            ListNode prev = newHead; // 尾插操作的尾指针
            int t = 0; // 记录进位
            while (cur1 != null || cur2 != null || t != 0) {
                // 先加上第⼀个链表
                if (cur1 != null) {
                    t += cur1.val;
                    cur1 = cur1.next;
                }
                // 加上第⼆个链表
                if (cur2 != null) {
                    t += cur2.val;
                    cur2 = cur2.next;
                }
                prev.next = new ListNode(t % 10);
                prev = prev.next;
                t /= 10;
            }
            return newHead.next;
        }
    }
    public static void main() {
        String[] args = new String[3];
    }
}
